#pragma once
#include<iostream>
#include<unistd.h>
using namespace std;

typedef void(*work_t)(int ); // 函数指针重定义
typedef void(*task_t)(int,pid_t); // 函数指针重定义



void PrintLog(int fd,pid_t id)
{
    cout<<"PringLog task"<<"sub_process:"<<id <<" fd:"<<fd<<endl;
}
void ReLoadConf(int fd,pid_t id)
{   
    cout<<"ReLoadConf task"<<"sub_process:"<<id <<" fd:"<<fd<<endl;
}
void ConnectMysql(int fd,pid_t id)
{
    cout<<"ConnectMysql task"<<"sub_process:"<<id <<" fd:"<<fd<<endl;
}

task_t tasks[] = {PrintLog,ReLoadConf,ConnectMysql};

uint32_t NextTask()
{
    uint32_t command_code = rand()%3;
    return command_code;
}

void worker(int fd)
{ 
    // 从pipe[0]中读取任务.
    while(true){
        
        u_int32_t command_code = 0; // 命令码,tasks数组下标.
        ssize_t n = read(0,&command_code,sizeof(command_code));
        if(n == sizeof(command_code)){
            if(command_code >=3){
                continue;
            }
            // 定向的让子进程完成对应的任务.
            tasks[command_code](fd,getpid());
        }else if( n ==0 ) // n==0 说明写端关闭了
        {
            cout<<"sbuProcess: "<<getpid()<<" quit Now"<<endl;
            break;
        }
        sleep(1);
    }
}